<template>
  <div class="tool-correct" v-loading="loading" style="min-height: 300px">
    <div class="step-content container">
      <el-form :inline="true">
        <el-form-item label="基数：">
          <el-input type="number" v-model="formData.base" />
        </el-form-item>
        <el-form-item label="设备号：">
          <el-input v-model="formData.deviceNo" placeholder="" />
        </el-form-item>
        <el-form-item label="本次校准周期开始时间：">
          <el-date-picker
            type="datetime"
            v-model="formData.startTime"
            placeholder=""
            clearable
            format="YYYY/MM/DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="本次校准周期结束时间：">
          <el-date-picker
            type="datetime"
            v-model="formData.endTime"
            placeholder=""
            clearable
            format="YYYY/MM/DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="startCompute">开始计算</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        border
        v-loading="loading"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="100" align="center" label="序号" fixed>
          <template v-slot="scope">
            {{ indexMethod(scope.$index, query.page, query.pageSize) }}
          </template>
        </el-table-column>
        <el-table-column label="全选" type="selection" width="55" />
        <template v-for="item in columns">
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            align="center"
            v-if="item.prop === 'deviceStatus'"
          >
            <template v-slot="scope">
              {{ ["在线", "离线"][scope.row[item.prop]] }}
            </template>
          </el-table-column>
          <el-table-column v-else-if="item.prop === 'option'" label="操作">
            <template v-slot="{ row }">
              <el-button type="warning" @click="openEdit(row)"
                >修改校准系数</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            v-else
            :prop="item.prop"
            :label="item.label"
            align="center"
          ></el-table-column>
        </template>
      </el-table>

      <el-pagination
        style="margin-top: 20px"
        background
        layout="prev, pager, next"
        @current-change="query.change"
        :total="query.total"
      >
      </el-pagination>
    </div>

    <el-dialog v-model="editShow" title="修改校准系数" width="400px" top="25vh">
      <el-form>
        <el-form-item label="系数">
          <el-input v-model="editNum" type="number" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="confirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { indexMethod } from "@/utils/tools"
import {
  getDeviceCalibrationCountRatio,
  getDeviceCalibrationDetailList,
  updateCalibrationFactor
} from "@/api"
import { useTool } from "./correctComponent/useTool"
import { Loading } from "@element-plus/icons-vue"
import { ElIcon, ElMessage } from "element-plus"

const { selectIds, selectItem } = useTool()

const query = reactive({
  total: 0,
  pageSize: 10,
  page: 1,
  change: (page) => {
    query.page = page
    getData()
  }
})
const formData = reactive({
  startTime: "",
  endTime: "",
  base: "",
  deviceNo: ""
})
useTableHight()
const emits = defineEmits(["change"])

const multipleTable = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
  selectItem.value = val
  selectIds.value = val.map((i) => i.deviceNo)
  console.log(multipleSelection.value, selectIds.value)
}

const tableData = ref([])
const columns = ref([
  { prop: "deviceNo", label: "设备编号" },
  { prop: "calibrationStartTime", label: "校准开始时间" },
  { prop: "calibrationEndTime", label: "校准结束时间" },
  { prop: "deviceMeteredElectricity", label: "设备计量电量" },
  { prop: "baseAmmeteMeteredElectricity", label: "基数电表计量电量" },
  { prop: "phaseSequence", label: "相序" },
  { prop: "calibrationFactor", label: "校准系数" },
  { prop: "option" }
])
const loading = ref(true)
const getData = async () => {
  selectItem.value = []
  loading.value = true
  const res = await getDeviceCalibrationDetailList({
    pageNumber: query.page,
    pageSize: query.pageSize
  })
  loading.value = false
  query.total = res?.total
  tableData.value = res?.rows
  console.log(res)
}

const next = () => {
  if (!selectItem.value.length) {
    ElMessage.warning("请至少选择一个设备")
    return
  }
  emits("change", 1)
}

const startCompute = async () => {
  const res = await getDeviceCalibrationCountRatio(formData)
  if (res) {
    getData()
  }
}

const editShow = ref(false)
const editNum = ref(0)
const editItem = ref({
  deviceNo: "",
  phaseSequence: ""
})

const openEdit = (row) => {
  editShow.value = true
  editItem.value = row
}

const close = () => {
  editShow.value = false
  editNum.value = 0
}
const confirm = async () => {
  const item = editItem.value

  const data = {
    deviceNo: item.deviceNo,
    phaseSequence: item.phaseSequence,
    num: editNum.value
  }
  const res = await updateCalibrationFactor(data)
  if (res) {
    getData()
    close()
  }
}

onMounted(() => {
  getData()
})
</script>
<style lang="less" scoped></style>
